//
//  ViewController.m
//  三级联动
//
//  Created by whz on 17/2/28.
//  Copyright © 2017年 whz. All rights reserved.
//

#import "ViewController.h"

#define KCELLID1 @"cellID1"
#define KCELLID2 @"cellID2"
#define KCELLID3 @"cellID3"
#define KTABLEVIEWHEIGHT 300

@interface ViewController () <UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, strong)UIButton *btn;
@property(nonatomic, strong)UIView *animateView;
@property(nonatomic, strong)UITableView *tableView1;
@property(nonatomic, strong)UITableView *tableView2;
@property(nonatomic, strong)UITableView *tableView3;
@property(nonatomic, strong)UIView *backView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithRed:231 / 255.0 green:231 / 255.0 blue:231 / 255.0 alpha:1.0];;
    
    [self setupUI];
    
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (self.tableView1) {
        return 5;
    } else if (self.tableView2) {
        return 10;
    } else {
        return 20;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (tableView == self.tableView1) {
        
        UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:KCELLID1 forIndexPath:indexPath];
        cell1.textLabel.text = @"whz1";
        return cell1;
        
    } else if (tableView == self.tableView2) {
    
        UITableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:KCELLID2 forIndexPath:indexPath];
        cell2.textLabel.text = @"whz2";
        return cell2;
        
    } else {
        
        UITableViewCell *cell3 = [tableView dequeueReusableCellWithIdentifier:KCELLID3 forIndexPath:indexPath];
        cell3.textLabel.text = @"whz3";
        return cell3;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if (tableView == self.tableView1) {
        
        [UIView animateWithDuration:0.5 animations:^{
            self.tableView2.frame = CGRectMake(80, 0, self.view.frame.size.width - 80, KTABLEVIEWHEIGHT);
        }];
        [self.tableView2 reloadData];

    }
    if (tableView == self.tableView2) {
        
        [UIView animateWithDuration:0.5 animations:^{
            self.tableView3.frame = CGRectMake(160, 0, self.view.frame.size.width - 160, KTABLEVIEWHEIGHT);
        }];
    }
}

- (void)startBtn {
    
    [UIView animateWithDuration:0.25 animations:^{
        self.animateView.alpha = 1;
        self.backView.alpha = 0.3;
        self.animateView.frame = CGRectMake(0, 100, self.view.frame.size.width, KTABLEVIEWHEIGHT);
        self.tableView1.frame = CGRectMake(0, 0, self.animateView.frame.size.width, KTABLEVIEWHEIGHT);
    }];
}

- (void)tap:(UITapGestureRecognizer *)sender {

    [UIView animateWithDuration:0.25 animations:^{
        self.animateView.frame = CGRectMake(0, 100, 0, 0);
        self.animateView.alpha = 0;
        _backView.alpha = 0;
    } completion:^(BOOL finished) {
        self.tableView2.frame = CGRectMake(self.view.frame.size.width, 0, 0, KTABLEVIEWHEIGHT);
        self.tableView3.frame = CGRectMake(self.view.frame.size.width, 0, 0, KTABLEVIEWHEIGHT);
    }];
}

- (void)setupUI {
    
    self.btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 64, 100, 35)];
    [self.view addSubview:self.btn];
    [self.btn setTitle:@"动画" forState:UIControlStateNormal];
    [self.btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.btn.titleLabel.font = [UIFont systemFontOfSize:15];
    self.btn.backgroundColor = [UIColor grayColor];
    [self.btn addTarget:self action:@selector(startBtn) forControlEvents:UIControlEventTouchUpInside];
    
    self.backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    self.backView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
    [self.backView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)]];
    self.backView.alpha = 0;
    [self.view addSubview:self.backView];
    
    self.animateView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 0, 0)];
    [self.view addSubview:self.animateView];
    
    self.tableView1 = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    [self.animateView addSubview:self.tableView1];
    self.tableView1.delegate = self;
    self.tableView1.dataSource = self;
    [self.tableView1 registerClass:[UITableViewCell class] forCellReuseIdentifier:KCELLID1];
    
    self.tableView2 = [[UITableView alloc] initWithFrame:CGRectMake(self.view.frame.size.width, 0, 0, KTABLEVIEWHEIGHT)];
    [self.animateView addSubview:self.tableView2];
    self.tableView2.delegate = self;
    self.tableView2.dataSource = self;
    [self.tableView2 registerClass:[UITableViewCell class] forCellReuseIdentifier:KCELLID2];
    
    self.tableView3 = [[UITableView alloc] initWithFrame:CGRectMake(self.view.frame.size.width, 0, 0, KTABLEVIEWHEIGHT)];
    [self.animateView addSubview:self.tableView3];
    self.tableView3.delegate = self;
    self.tableView3.dataSource = self;
    [self.tableView3 registerClass:[UITableViewCell class] forCellReuseIdentifier:KCELLID3];
}

@end
